import { AxiosInstance} from 'axios';
import axios from 'axios'

const instance: AxiosInstance = axios.create({
  baseURL: 'http://localhost:8081/goods',
  timeout: 2000,
  headers: {
    'Content-type': 'application/json'
  },

  withCredentials: true
})

async function findAllGoods(map: {name?: string, typename?: string}) {
  let response = await instance.get('/', {
    params: {
      name: map.name,
      typename: map.typename
    }
  })

  return response.data
}

async function findGoods(id: number) {
  let response = await instance.get(`/${id}`)
  return response.data
}

async function insertOneGoods(goods: Goods) {
  let response = await instance.post('/', JSON.stringify(goods))
  return response.data
}

async function deleteOneGoods(id: number) {
  let response = await instance.delete('/', {
    params: {
      id
    }
  })
  return response.data
}

async function updateOneGoods(goods: Goods) {
  let response = await instance.put('/', JSON.stringify(goods))
  return response.data
}

export {
  findAllGoods,
  findGoods,
  insertOneGoods,
  deleteOneGoods,
  updateOneGoods
}